package com.cidp.system.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.TopLog;
import com.cidp.system.model.Toperator;
import com.cidp.system.pojo.OpLogPo;
import com.cidp.system.pojo.OperatorPo;
import com.cidp.system.service.IOpLogService;
import com.cidp.system.service.IOperatorService;
import com.litt.core.common.Constants;
import com.litt.core.exception.BusiException;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;

/**
 * <b>标题：</b>操作日志.
 * <pre><b>描述</b>
 *    操作日志管理
 * </pre>
 * 
 * @author lijian
 * @since 2008-6-3
 * @version 1.0
 */
public class OpLogAction extends DispatchAction{
	
	private IOpLogService opLogService;
	private IOperatorService operatorService;
	/**
	 * 查看操作日志
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OpLogPo opLogPo = (OpLogPo)dynaActionForm.get("opLogPo");
		
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = opLogService.listByPage(new PagePoParam(opLogPo, limit.getPage(), limit.getCurrentRowsDisplayed()));

		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("toList");
	}

	/**
	 * 保存操作日志
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OpLogPo opLogPo = (OpLogPo)dynaActionForm.get("opLogPo");
		
		TopLog opLog = new TopLog();
		opLog.setContent(opLogPo.getContent());
		opLog.setFuncCode(opLogPo.getFuncCode());
		opLog.setSerialNo(opLogPo.getSerialNo());
		
		opLogService.saveTopLog(opLog);
		
		return mapping.findForward("toSave");
	}
	
	/**
	 * 新增操作日志
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 修改操作日志
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OpLogPo opLogPo = (OpLogPo)dynaActionForm.get("opLogPo");
		
		String opId = request.getParameter("opId");
		TopLog opLog = opLogService.loadById(new Long(opId));
		try{
			BeanUtils.copyProperties(opLogPo, opLog);
		}catch(Exception e){
			
		}
		dynaActionForm.set("opLogPo", opLogPo);
		return mapping.findForward("toEdit");
	}
	
	/**
	 * 更新操作日志
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OpLogPo opLogPo = (OpLogPo)dynaActionForm.get("opLogPo");
		
		TopLog opLog = opLogService.loadById(opLogPo.getOpId());
		opLog.setContent(opLogPo.getContent());
		opLog.setFuncCode(opLogPo.getFuncCode());
		opLog.setOpDatetime(new Date());
		opLog.setSerialNo(opLogPo.getSerialNo());
		
		opLogService.updateTopLog(opLog);
		
		return mapping.findForward("toUpdate");
	}
	
	/**
	 * 查看操作员信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward showOperator(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		OperatorPo operatorPo = (OperatorPo)dynaActionForm.get("operatorPo");
		
		String id = request.getParameter("id");
		Toperator operator = operatorService.load(new Long(id));
		try{
			BeanUtils.copyProperties(operator, operatorPo);
		}catch(Exception e){
			throw new BusiException("对象与Po转换失败！");
		}

		dynaActionForm.set("operatorPo", operatorPo);
		return mapping.findForward("toOperatorDetail");
	}
	

	public void setOpLogService(IOpLogService opLogService) {
		this.opLogService = opLogService;
	}

	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}

}
